package com.apes.fn.scm.vipSettle.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.area.model.BusinessCircle;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.AccessProvider;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * @Title: VipUnionSettle
 * @Description: 会员联盟结算表
 * @author: cyy
 * @create: 2022/7/01 14:42
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VipUnionSettle.CONST_TABLE_NAME)
public class VipUnionSettle extends Model {

    public static final String CONST_TABLE_NAME = "vip_union_settle";

    @Id
    @Column(name = "id",nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "会员联盟结算表主表编码", padding = 8, prefix = "HM")
    @Comment("编码")
    private String id;

    @NotNull
    @Column(name = "retail_order_id")
    @Comment("付款单编码")
    private String retailOrder;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "receipt_date")
    @Comment("收款日期")
    private Date receiptDate;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "statistic_date")
    @Comment("统计日期")
    private Date statisticDate;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_company_id", referencedColumnName = "id")
    @Comment("对方公司")
    private PartyRole partnerCompany;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("部门")
    private Dept dept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_dept_id", referencedColumnName = "id")
    @Comment("对方部门")
    private Dept partnerDept;

    @Column(name = "type", length = 128)
    @Comment("类型")
    private String type; // 收入(in),支出(out)

    @Column(name = "origin_business_type", length = 32)
    @Comment("源单据类型")
    private String originBusinessType;

    @Column(name = "origin", length = 30)
    @Comment("源单号")
    private String origin;

    @Column(name = "source_business_type", length = 125)
    @Comment("上游单据类型")
    private String sourceBusinessType;

    @Column(name = "source", length = 30)
    @Comment("上游单号")
    private String source;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vip_alliance", referencedColumnName = "id")
    @Comment("会员联盟组")
    private Enumeration vipAlliance;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_circle_id", referencedColumnName = "id")
    @Comment("商圈")
    private BusinessCircle businessCircle;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "access_provider", referencedColumnName = "id")
    @Comment("公司接入商")
    private AccessProvider accessProvider;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "other_access_provider", referencedColumnName = "id")
    @Comment("对方公司接入商")
    private AccessProvider otherAccessProvider;

    @Column(name = "payment_method_type", length = 128)
    @Comment("收款方式类型")
    private String paymentMethodType; // 储值(balance),折扣(discount)

    @Column(name = "settlement_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("应结金额")
    private double settlementAmount = 0;

    @Column(name = "use_savings", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("使用储值")
    private double useSavings = 0;

    @Column(name = "give_savings", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("储值应结")
    private double giveSavings = 0;

    @Column(name = "platform_management_fee", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("平台管理费")
    private double platformManagementFee = 0;

    @Column(name = "car_license", length = 255)
    @Comment("车牌")
    private String carLicense;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_customer_expand_id", referencedColumnName = "id")
    @Comment("赋能客户车辆扩展信息")
    private FnCustomerExpand fnCustomerExpand;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "platform_company_id", referencedColumnName = "id")
    @Comment("平台公司")
    private PartyRole platformCompany;

    @Column(name = "state")
    @Comment("状态")
    private String state;

    @OneToMany(mappedBy = "vipUnionSettle", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<VipUnionSettleItem> vipUnionSettleItems = new HashSet<>();

    public Set<VipUnionSettleItem> getVipUnionSettleItems() {
        if(vipUnionSettleItems == null) return new HashSet<>();
        for (VipUnionSettleItem item : vipUnionSettleItems) {
            item.setVipUnionSettle(this);
        }
        return vipUnionSettleItems;
    }
}
